package edu.drexel.cs575.phw1.scale;

import java.util.ArrayList;
import java.util.List;

import listeners.IdleTimerListener;

/**
 * Keeps track of the time which has passed without activity. 
 * 
 * @author anthony
 *
 */
public class IdleTimer {
	private List<IdleTimerListener> listeners = new ArrayList<IdleTimerListener>();
	private int timeout = 120; //seconds
	
	public void start() {
		System.out.println("IDLE_TIMER: Start.");
	}
	
	public void stop() {
		System.out.println("IDLE_TIMER: Stop.");
	}
	
	public void reset() {
		//a real implementation would set timeout back to zero.
		System.out.println("IDLE_TIMER: Reset.");
	}
	
	public void setTimeout(int timeout) {
		this.timeout = timeout;
	}
	
	public void addListener(IdleTimerListener listener) {
		if (listener == null) {
			throw new IllegalArgumentException("Listener cannot be null");
		}
		
		this.listeners.add(listener);
	}
	
	public void notifyTimeExpiredListeners() {
		//time expires if the timer ever reaches the timout value
		for(IdleTimerListener listener : listeners) {
			listener.timeExpired();
		}
	}
}
